<template>
  <div>
    <el-form
      ref="registerForm"
      :model="registerData"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item prop="username" label="用户名：">
        <el-input
          v-model="registerData.username"
          type="text"
          autocomplete="on"
          placeholder="请输入用户名"
          clearable
        >
          <i slot="prefix" class="el-input__icon el-icon-user" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码：">
        <el-input
          v-model="registerData.password"
          type="password"
          autocomplete="on"
          placeholder="请输入密码"
          clearable
        >
          <i slot="prefix" class="el-input__icon el-icon-lock" />
        </el-input>
      </el-form-item>
      <el-form-item prop="checkPassword" label="确认密码：">
        <el-input
          v-model="registerData.checkPassword"
          type="password"
          autocomplete="on"
          placeholder="请再次输入密码"
          clearable
        >
          <i slot="prefix" class="el-input__icon el-icon-lock" />
        </el-input>
      </el-form-item>
      <el-form-item prop="phoneNum" label="手机号：">
        <el-input
          v-model="registerData.phoneNum"
          type="text"
          autocomplete="on"
          placeholder="请输入手机号"
          clearable
        >
          <i slot="prefix" class="el-input__icon el-icon-phone-outline" />
        </el-input>
      </el-form-item>
    </el-form>
    <div class="bottomBtn">
      <el-button type="primary" round @click="submitRegister">
        注册
      </el-button><br>
      <span class="goReg" @click="goback()"><i class="el-icon-back" /> 返回登录 </span>
    </div>
  </div>
</template>
<script>
import { checkUserName, register } from '@/api/user';
import { transformTimestamp } from '@/utils/validate.js';
export default {
  name: 'RegisterView',
  props: { goback: { type: Function, default: () => {} } },
  data() {
    var validateUser = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'));
      } else {
        checkUserName(value).then(res => {
          if (res.data.code === 200) {
            callback(new Error('账号已被注册'));
          } else {
            callback();
          }
        });
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerData.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号码'));
      } else if (!(/^[1][3,4,5,7,8,9][0-9]{9}$/.test(value))) {
        callback(new Error('请输入正确的手机号码'));
      } else {
        callback();
      }
    };
    return {
      registerData: {
        username: '',
        password: '',
        phoneNum: '',
        checkPassword: ''
      },
      rules: {
        username: [
          { required: true, validator: validateUser, trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        phoneNum: [
          { required: true, validator: validatePhone, trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitRegister() {
      this.$refs['registerForm'].validate((valid) => {
        if (valid) {
          const params = this.registerData;
          params.addDate = transformTimestamp(new Date());
          register(params).then(res => {
            if (res.data.code === 200) {
              this.$message({
                showClose: true,
                message: '注册成功！',
                type: 'success'
              });
              this.goback();
            }
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="sass">

</style>
